using StackCalc.Instructions;

namespace StackCalc.Parsing.ParsingUnits
{
	public class SpecialCharacterParser : BaseParsingUnit<PushCharacter>
	{
		protected override bool CanParseImplementation(string input)
		{
			// has to start with '\'
			return input[0] == '\\';
		}

		protected override bool HasSyntaxErrorImplementation(string input)
		{
			// syntax error if input length is only one
			return input.Length == 1;
		}

		public override string GetErrorDescription()
		{
			return "'\\' needs to be followed by a character!";
		}

		protected override PushCharacter ParseImplementation(string input)
		{
			char character = input[1];

			// return \n and \t as their special ASCII code
			if (character == 'n')
			{
				return new PushCharacter('\n');
			}
			else if (character == 't')
			{
				return new PushCharacter('\t');
			}
			else
			{
				// return all other characters simply as their character
				return new PushCharacter(input[1]);
			}
		}

		protected override string TrunkateImplementation(string input)
		{
			return input.Remove(0, 2);
		}
	}
}
